<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="动态内容的地理位置" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入动态内容的地理位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="动态内容的地理位置" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入动态内容的地理位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布动态的用户 ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入发布动态的用户 ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="动态内容的评论数" prop="commentsCount">
        <el-input
          v-model="queryParams.commentsCount"
          placeholder="请输入动态内容的评论数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="动态内容的点击量或浏览量" prop="clicksCount">
        <el-input
          v-model="queryParams.clicksCount"
          placeholder="请输入动态内容的点击量或浏览量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="动态内容的点赞数" prop="likesCount">
        <el-input
          v-model="queryParams.likesCount"
          placeholder="请输入动态内容的点赞数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="动态内容的推荐分数" prop="recommendScore">
        <el-input
          v-model="queryParams.recommendScore"
          placeholder="请输入动态内容的推荐分数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="动态内容的分享次数" prop="shareCount">
        <el-input
          v-model="queryParams.shareCount"
          placeholder="请输入动态内容的分享次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="动态内容的收藏次数" prop="favoriteCount">
        <el-input
          v-model="queryParams.favoriteCount"
          placeholder="请输入动态内容的收藏次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="动态内容的分类" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入动态内容的分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['smile:content:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['smile:content:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['smile:content:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['smile:content:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="动态内容唯一标识" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="内容或描述" align="center" prop="content" width="600" max-height="55" show-overflow-tooltip="true"/>
      <el-table-column label="封面图片的 URL" align="center" prop="icon" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.icon" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="资源地址" align="center" prop="uri" />
      <el-table-column label="动态内容的地理位置" align="center" prop="province" />
      <el-table-column label="动态内容的地理位置" align="center" prop="city" />
      <el-table-column label="发布动态的用户 ID" align="center" prop="userId" />
      <el-table-column label="动态的样式类型" align="center" prop="style">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dynamic_content_style_classification" :value="scope.row.style"/>
        </template>
      </el-table-column>
      <el-table-column label="动态内容的评论数" align="center" prop="commentsCount" />
      <el-table-column label="动态内容的点击量或浏览量" align="center" prop="clicksCount" />
      <el-table-column label="动态内容的点赞数" align="center" prop="likesCount" />
      <el-table-column label="否点赞过该动态" align="center" prop="likeId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.liked_unliked" :value="scope.row.likeId"/>
        </template>
      </el-table-column>
      <el-table-column label="当前用户是否收藏了该动态" align="center" prop="isFavorited">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.collected_not_collected" :value="scope.row.isFavorited"/>
        </template>
      </el-table-column>
      <el-table-column label="当前用户是否分享了该动态" align="center" prop="isShared">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shared_unshared" :value="scope.row.isShared"/>
        </template>
      </el-table-column>
      <el-table-column label="当前用户是否关注了发布该动态的用户" align="center" prop="isFollowed">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.attention_unattention" :value="scope.row.isFollowed"/>
        </template>
      </el-table-column>
      <el-table-column label="动态内容的来源" align="center" prop="source">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_upload_official_release_third_party_cooperation" :value="scope.row.source"/>
        </template>
      </el-table-column>
      <el-table-column label="动态内容的推荐分数" align="center" prop="recommendScore" />
      <el-table-column label="动态内容的分享次数" align="center" prop="shareCount" />
      <el-table-column label="动态内容的收藏次数" align="center" prop="favoriteCount" />
      <el-table-column label="动态内容的分类" align="center" prop="category" />
      <el-table-column label="动态内容的缩略图 URL" align="center" prop="thumbnail" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.thumbnail" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="视频动态的封面图  URL" align="center" prop="videoCover" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.videoCover" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="动态内容的宽度" align="center" prop="width" />
      <el-table-column label="动态内容的高度" align="center" prop="height" />
      <el-table-column label="动态内容的时长" align="center" prop="duration" />
      <el-table-column label="动态内容的纬度" align="center" prop="latitude" />
      <el-table-column label="动态内容的经度" align="center" prop="longitude" />
      <el-table-column label="视频动态的时长" align="center" prop="videoDuration" />
      <el-table-column label="音频动态的 URL" align="center" prop="audioUrl" />
      <el-table-column label="音频动态的时长" align="center" prop="audioDuration" />
      <el-table-column label="动态内容关联的商品 ID" align="center" prop="productId" />
      <el-table-column label="动态内容关联的活动 ID" align="center" prop="activityId" />
      <el-table-column label="动态内容的外部链接" align="center" prop="externalLink" />
      <el-table-column label="动态内容是否公开" align="center" prop="isPublic">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.private_public" :value="scope.row.isPublic"/>
        </template>
      </el-table-column>
      <el-table-column label="动态内容是否置顶" align="center" prop="isPinned">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.top_no_top" :value="scope.row.isPinned"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.normal_under_review_removed" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="过期时间" align="center" prop="expireTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['smile:content:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['smile:content:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改动态内容对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容或描述">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="封面图片的 URL" prop="icon">
          <image-upload v-model="form.icon"/>
        </el-form-item>
        <el-form-item label="资源地址" prop="uri">
          <file-upload v-model="form.uri"/>
        </el-form-item>
        <el-form-item label="动态内容的地理位置" prop="province">
          <el-input v-model="form.province" placeholder="请输入动态内容的地理位置" />
        </el-form-item>
        <el-form-item label="动态内容的地理位置" prop="city">
          <el-input v-model="form.city" placeholder="请输入动态内容的地理位置" />
        </el-form-item>
        <el-form-item label="发布动态的用户 ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入发布动态的用户 ID" />
        </el-form-item>
        <el-form-item label="动态的样式类型" prop="style">
          <el-select v-model="form.style" placeholder="请选择动态的样式类型">
            <el-option
              v-for="dict in dict.type.dynamic_content_style_classification"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="动态内容的评论数" prop="commentsCount">
          <el-input v-model="form.commentsCount" placeholder="请输入动态内容的评论数" />
        </el-form-item>
        <el-form-item label="动态内容的点击量或浏览量" prop="clicksCount">
          <el-input v-model="form.clicksCount" placeholder="请输入动态内容的点击量或浏览量" />
        </el-form-item>
        <el-form-item label="动态内容的点赞数" prop="likesCount">
          <el-input v-model="form.likesCount" placeholder="请输入动态内容的点赞数" />
        </el-form-item>
        <el-form-item label="否点赞过该动态" prop="likeId">
          <el-radio-group v-model="form.likeId">
            <el-radio
              v-for="dict in dict.type.liked_unliked"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="当前用户是否收藏了该动态" prop="isFavorited">
          <el-radio-group v-model="form.isFavorited">
            <el-radio
              v-for="dict in dict.type.collected_not_collected"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="当前用户是否分享了该动态" prop="isShared">
          <el-radio-group v-model="form.isShared">
            <el-radio
              v-for="dict in dict.type.shared_unshared"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="当前用户是否关注了发布该动态的用户" prop="isFollowed">
          <el-radio-group v-model="form.isFollowed">
            <el-radio
              v-for="dict in dict.type.attention_unattention"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="动态内容的来源" prop="source">
          <el-select v-model="form.source" placeholder="请选择动态内容的来源">
            <el-option
              v-for="dict in dict.type.user_upload_official_release_third_party_cooperation"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="动态内容的推荐分数" prop="recommendScore">
          <el-input v-model="form.recommendScore" placeholder="请输入动态内容的推荐分数" />
        </el-form-item>
        <el-form-item label="动态内容的分享次数" prop="shareCount">
          <el-input v-model="form.shareCount" placeholder="请输入动态内容的分享次数" />
        </el-form-item>
        <el-form-item label="动态内容的收藏次数" prop="favoriteCount">
          <el-input v-model="form.favoriteCount" placeholder="请输入动态内容的收藏次数" />
        </el-form-item>
        <el-form-item label="动态内容的分类" prop="category">
          <el-input v-model="form.category" placeholder="请输入动态内容的分类" />
        </el-form-item>
        <el-form-item label="动态内容的缩略图 URL" prop="thumbnail">
          <image-upload v-model="form.thumbnail"/>
        </el-form-item>
        <el-form-item label="视频动态的封面图  URL" prop="videoCover">
          <image-upload v-model="form.videoCover"/>
        </el-form-item>
        <el-form-item label="动态内容的宽度" prop="width">
          <el-input v-model="form.width" placeholder="请输入动态内容的宽度" />
        </el-form-item>
        <el-form-item label="动态内容的高度" prop="height">
          <el-input v-model="form.height" placeholder="请输入动态内容的高度" />
        </el-form-item>
        <el-form-item label="动态内容的时长" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入动态内容的时长" />
        </el-form-item>
        <el-form-item label="动态内容的纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入动态内容的纬度" />
        </el-form-item>
        <el-form-item label="动态内容的经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入动态内容的经度" />
        </el-form-item>
        <el-form-item label="视频动态的时长" prop="videoDuration">
          <el-input v-model="form.videoDuration" placeholder="请输入视频动态的时长" />
        </el-form-item>
        <el-form-item label="音频动态的 URL" prop="audioUrl">
          <file-upload v-model="form.audioUrl"/>
        </el-form-item>
        <el-form-item label="音频动态的时长" prop="audioDuration">
          <el-input v-model="form.audioDuration" placeholder="请输入音频动态的时长" />
        </el-form-item>
        <el-form-item label="动态内容关联的商品 ID" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入动态内容关联的商品 ID" />
        </el-form-item>
        <el-form-item label="动态内容关联的活动 ID" prop="activityId">
          <el-input v-model="form.activityId" placeholder="请输入动态内容关联的活动 ID" />
        </el-form-item>
        <el-form-item label="动态内容的外部链接" prop="externalLink">
          <el-input v-model="form.externalLink" placeholder="请输入动态内容的外部链接" />
        </el-form-item>
        <el-form-item label="动态内容是否公开" prop="isPublic">
          <el-input v-model="form.isPublic" placeholder="请输入动态内容是否公开" />
        </el-form-item>
        <el-form-item label="动态内容是否置顶" prop="isPinned">
          <el-input v-model="form.isPinned" placeholder="请输入动态内容是否置顶" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.normal_under_review_removed"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-radio-group v-model="form.delFlag">
            <el-radio
              v-for="dict in dict.type.presence_deletion"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="过期时间" prop="expireTime">
          <el-date-picker clearable
            v-model="form.expireTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择过期时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContent, getContent, delContent, addContent, updateContent } from "@/api/smile/content";

export default {
  name: "Content",
  dicts: ['liked_unliked', 'shared_unshared', 'user_upload_official_release_third_party_cooperation', 'presence_deletion', 'collected_not_collected', 'normal_under_review_removed', 'dynamic_content_style_classification', 'attention_unattention'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 动态内容表格数据
      contentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        province: null,
        city: null,
        userId: null,
        commentsCount: null,
        clicksCount: null,
        likesCount: null,
        recommendScore: null,
        shareCount: null,
        favoriteCount: null,
        category: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "发布动态的用户 ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询动态内容列表 */
    getList() {
      this.loading = true;
      listContent(this.queryParams).then(response => {
        this.contentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        content: null,
        icon: null,
        uri: null,
        province: null,
        city: null,
        userId: null,
        style: null,
        commentsCount: null,
        clicksCount: null,
        likesCount: null,
        likeId: null,
        isFavorited: null,
        isShared: null,
        isFollowed: null,
        source: null,
        recommendScore: null,
        shareCount: null,
        favoriteCount: null,
        category: null,
        thumbnail: null,
        videoCover: null,
        width: null,
        height: null,
        duration: null,
        latitude: null,
        longitude: null,
        videoDuration: null,
        audioUrl: null,
        audioDuration: null,
        productId: null,
        activityId: null,
        externalLink: null,
        isPublic: null,
        isPinned: null,
        status: null,
        delFlag: null,
        expireTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加动态内容";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改动态内容";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除动态内容编号为"' + ids + '"的数据项？').then(function() {
        return delContent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('smile/content/export', {
        ...this.queryParams
      }, `content_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
